package org.kl.bf.service.basic;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.kl.bf.entity.basic.Post;
import org.kl.bf.repository.basic.PostDao;
import org.kl.bf.utils.BasicConstants;
import org.kl.bf.utils.CRUDUtils;
import org.kl.bf.utils.KLDynamicSpecifications;
import org.kl.bf.utils.KLSearchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author ymc
 * @CreateTime 2014-3-13 下午9:11:30
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional
public class PostService {

	private PostDao postDao;

	public Post getPost(String guid) {
		return postDao.find(Post.class,guid);
	}
	
	public Post getOnePost(String guid){
		return postDao.findOne(guid);
	}

	public void savePost(Post entity) {
		postDao.save(entity);
	}

	public void deletePost(String guid) {
		postDao.delete(guid);
	}

	public List<Post> getAllPost() {
		return postDao.findAll();
	}

	public List<Post> findByOrganizationIdAndEnable(String id)
	{
		return postDao.findByOrganizationIdAndEnable(id,BasicConstants.YES_INT);
		
	}
	public List<Post> findByOrganizationId(String id)
	{
		return postDao.findByOrganizationId(id);
		
	}
	public Post findByPostNoAndOrganizationId(String postNo,String id){
		return postDao.findByPostNoAndOrganizationId(postNo, id);
		
	}
	public Post findByPostNameAndOrganizationId(String postName,String id){
		return postDao.findByPostNameAndOrganizationId(postName, id);
		
	}
	
	public List<Post> findByHigherPostId(String id){
		return postDao.findByHigherPostId(id);
	}
	
	/**
	 * 删除多条记录
	 * 
	 * @param ids
	 */
	public String multiDelete(String ids) {
		String[] idArray = StringUtils.split(ids, ",");
		int i = 0;
		for (String id : idArray) {
			if (StringUtils.isNotBlank(id)) {
				//Staffinfo staffinfo  = 
				postDao.delete(id);
				i++;
			}
		}
		return "删除" + i + "条记录";
	}

	
	public Page<Post> getPostInfo(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType,
			String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<Post> spec = buildSpecification(searchParams);
		Page<Post> postp = postDao.findAll(spec, pageRequest);
		Iterator<Post> iterator = postp.iterator();
		while(iterator.hasNext())
		{
			Post po = iterator.next();
			if(po.getHigherPostId() != null)
			{
				Post p = new Post();
				p = postDao.findOne(po.getHigherPostId());
				po.setHigherPost(p);
			}
		}
		return postp;
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<Post> buildSpecification(Map<String, Object> searchParams) {
		Map<String, KLSearchFilter> filters = KLSearchFilter.parse(searchParams);
		Specification<Post> spec = KLDynamicSpecifications.byKLSearchFilter(filters.values(), Post.class);
		return spec;
	}

	@Autowired
	public void setPostDao(PostDao postDao) {
		this.postDao = postDao;
	}

	public List<Post> getAllPostInfo(Map<String, Object> searchParams,String sortType, String orderType) {
		Specification<Post> spec = KLDynamicSpecifications.buildSpecification(searchParams, Post.class);
		return postDao.findAll(spec, CRUDUtils.buildSort(sortType, orderType));
	}

}
